package com.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group.entity.ProjectInside;
import com.group.entity.TalentApplyProject;
import com.group.entity.Talents;
import com.group.mapper.GroupsInformationMapper;
import com.group.mapper.ProjectOutsideMapper;
import com.group.mapper.TalentApplyProjectMapper;
import com.group.mapper.TalentsMapper;
import com.group.service.GroupsInformationService;
import com.group.service.ProjectInsideService;
import com.group.service.TalentApplyProjectService;
import com.group.service.TalentsService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-09-09
 */
@Controller
//@RequestMapping("/talent-apply-project")
@SuppressWarnings("all")
public class TalentApplyProjectController {

    @Autowired
    TalentsMapper talentsMapper;

    @Autowired
    TalentApplyProjectService talentApplyProjectService;

    @Autowired
    TalentApplyProjectMapper talentApplyProjectMapper;

    @Autowired
    GroupsInformationService groupsInformationService;

    @Autowired
    ProjectInsideService projectInsideService;

    //项目广场申请入队
    @GetMapping("/applyJoinTeamInSquare")
    @ResponseBody
    public boolean applyJoinTeam(@RequestParam(value = "projectId", required = true) Integer projectId, @RequestParam(value = "masterId", required = true) Integer masterId, HttpSession session) {
        //获取当前申请项目的Id
        TalentApplyProject talentApplyProject = new TalentApplyProject();
        talentApplyProject.setProjectId(projectId);
        talentApplyProject.setMasterId(masterId);
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userId", currentUserId);
        Talents talents = talentsMapper.selectOne(wrapper);
        talentApplyProject.setUserId(talents.getUserId());
        talentApplyProject.setTalentTag(talents.getTalentTag());
        talentApplyProject.setTalentName(talents.getTalentName());
        talentApplyProject.setTalentIntro(talents.getTalentIntro());
        boolean save = false;
        try {
            save = talentApplyProjectService.save(talentApplyProject);
        } catch (Exception exception) {
            save = false;
        }
        return save;
    }

    //个人资料页我收到的入队申请
    @RequestMapping("/showTalentsApply")
    public String showTalentsApply(HttpSession session, Model model) {
        //拿到当前登录用户的userId
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("master_id", currentUserId);
        List talentsApplyList = talentApplyProjectMapper.selectList(wrapper);
        model.addAttribute("applyTalentsList", talentsApplyList);
        //去查询入队申请表
        return "receivedJoinTeamApply";
    }

    //队长同意申请
    @GetMapping("/acceptJoinTeam/{userId}/{projectId}")
    public String acceptJoinTeam(@PathVariable("userId") Integer userId, @PathVariable("projectId") Integer projectId) throws IllegalAccessException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("projectId", projectId);
        ProjectInside projectInside = projectInsideService.getOne(wrapper);
        groupsInformationService.addGroupMember(projectInside.getGroupId(),userId);
        Map map = new HashMap();
        map.put("projectId", projectId);
        map.put("userId", userId);
        talentApplyProjectService.removeByMap(map);
        return "redirect:/project-user/myProject";
    }

    /**
     * 队长拒绝申请，直接将用户从申请表中删除，
     *
     * @param userId
     * @return
     */
    @GetMapping("/refuseJoinTeam/{userId}/{projectId}")
    @ResponseBody
    public String refuseJoinTeam(@PathVariable("userId") Integer userId, @PathVariable("projectId") Integer projectId) {
        HashMap hashMap = new HashMap();
        hashMap.put("userId", userId);
        hashMap.put("projectId", projectId);
        talentApplyProjectMapper.deleteByMap(hashMap);
        return "success";
    }

}

